package genedit.model.graph.classModel;

import genedit.gui.exceptions.ElementNePostoji;
import genedit.model.graph.classModel.GAttribute.VisibilityType;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class GMethod implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -2640246901519207356L;
	private String returnType;
	private String name;
	private GParameters parameteres;
	private VisibilityType visibility;
	
	public String getReturnType() {
		return returnType;
	}

	public VisibilityType getVisibilityType() {
	return visibility;
	}
	public void setVisibilityType(VisibilityType visibility) {
		this.visibility = visibility;
		}
	
	public void setVisibilityType(String visi) {
		if (visi.equals("private")) {
			this.visibility = VisibilityType.priv;
		} else if(visi.equals("protected")) {
			this.visibility = VisibilityType.prot;
		} else {
			this.visibility = VisibilityType.publ;
		}
		}
	
	public String getVisibility() {
		if (visibility == VisibilityType.priv) {
			return "private";
		} else if (visibility == VisibilityType.prot) {
			return "protected";
		} else {
			return "public";
		}
	}
	public String getStringParameters() {
		if (parameteres == null)
			return "";
		else {
			String retval = "";
			int i = 0;
			for(String param : parameteres.getParameterKeys()){
				try {
				if(i != parameteres.getParameters().size()-1)					
						retval+= parameteres.getParameter(param).toString() + ",";					
				else
					retval+= parameteres.getParameter(param).toString();
					} catch (ElementNePostoji e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			i++;
			}
			return retval;
		}
	}
	
	public void setReturnType(String returnType) {
		this.returnType = returnType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public GParameters getParameteres() {
		if (parameteres == null)
			parameteres = new GParameters();
		return parameteres;
	}

	public void setParameteres(GParameters parameteres) {
		this.parameteres = parameteres;
	}

	public GMethod() {
		this.returnType = "";
		this.name = "";
		this.parameteres = new GParameters();
		this.visibility = VisibilityType.priv;
		
	}
	
	public GMethod(String retType, String name, String visi) {
		this.returnType = retType;
		this.name = name;
		this.parameteres = new GParameters();
		if (visi.equals("private")) {
			this.visibility = VisibilityType.priv;
		} else if(visi.equals("protected")) {
			this.visibility = VisibilityType.prot;
		} else {
			this.visibility = VisibilityType.publ;
		}
	}

	@Override
	public String toString() {
		String retVal;
		if (visibility == VisibilityType.priv) {
			retVal = "- ";
		} else if (visibility == VisibilityType.prot) {
			retVal = "# ";
		} else {
			retVal = "+ ";
		}

		retVal += name + "( ";

		int i = 0;
		for(String param : parameteres.getParameterKeys()){
			try {
			if(i != parameteres.getParameters().size()-1)					
				retVal+= parameteres.getParameter(param).toString() + ",";					
			else
				retVal+= parameteres.getParameter(param).toString();
				} catch (ElementNePostoji e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		i++;
		}

		retVal += " ) : " + returnType;

		return retVal;
	}
}
